#include "sqlProvider.h"
#include <future>

sqlProvider::sqlProvider()
{

}

void sqlProvider::Connect()
{
    if(currentStatus.GetInit(MU_DEVICE_SQL)){     /// 如果已经初始化了
        qWarning()<<"SQL已经初始化成功!"<<__FUNCTION__;
        return ;
    }
    currentStatus.setInitType(MU_DEVICE_SQL,MU_TY_RUNNING);
    std::future<MU_RETURNS> result = std::async(std::launch::async,&sqlProvider::_open,this);

    auto ret = result.get();


    if(MU_RETURN_SUCCESS != ret){
        currentStatus.initFailed(MU_DEVICE_SQL);
        std::cout << "DB连接:" << MU_Get_Msg(ret) << ".\n";
        return ;
    }
    currentStatus.initSucess(MU_DEVICE_SQL);

    SysMembers::GetInstance()->importALL();

}

QVariantList sqlProvider::Get(string query)
{
    QVariantList list;
    try {
        return SQLHelper::GetBlade()->_p_tr->Get(query);
    } catch (MU_EXPS::SQL_EXPECTION e) {
        /// 写入日志
        qWarning()<<BOLDRED<<__FUNCTION__<<"查表失败:"<<"\t"<<RESET;
        return list;
    }
}


MU_RETURNS sqlProvider::insert(TaskList task)
{
    /// 插入多条数据的方法
    std::string query = "insert into task(任务编号,电压,电流,速度,状态) values(:任务编号,:电压,:电流,:速度,:状态)";
    //SQLHelper::GetBlade()->_p_tr->prepare(query,task);
}

MU_RETURNS sqlProvider::insert(ResultList result)
{
    /// 插入多条数据的方法
    std::string query = "insert into task(任务编号,序号,width,height,radius,time) values(任务编号,序号,width,height,radius,time)";
    //SQLHelper::GetBlade()->_p_tr->prepare(query,result);
}

ResultList sqlProvider::transVarToResult(QVariantList r)
{
    return std::make_tuple(r.at(0).toInt(),r.at(1).toInt(),r.at(2).toDouble(),r.at(3).toDouble(),r.at(4).toDouble(),r.at(5).toString().toStdString());
}

TaskList sqlProvider::transVarToTask(QVariantList r)
{
    return std::make_tuple(r.at(0).toInt(),r.at(1).toDouble(),r.at(2).toDouble(),r.at(3).toInt(),r.at(4).toBool());
}



ResultList sqlProvider::getResult(int index)
{
    string query = "select * from " + tb_name + " where " + " " + to_string(index);
    try {
        auto t = SQLHelper::GetBlade()->_p_tr->Get(query);
        return transVarToResult(t);
    } catch (MU_EXPS::SQL_EXPECTION e) {
        qWarning()<<__FUNCTION__<<"获取结果索引失败:"<<index<<"\t";
        return std::make_tuple(-3,-3,0,0,0,"Invaild");
    }


}

TaskList sqlProvider::getTask(int index)
{
    string query = "select * from " + tb_name + " where " + " " + to_string(index);
    try {
        auto t = SQLHelper::GetBlade()->_p_tr->Get(query);
        return transVarToTask(t);
    } catch (MU_EXPS::SQL_EXPECTION e) {
        /// 写入日志
        qWarning()<<__FUNCTION__<<"获取任务索引失败:"<<index<<"\t";
        return std::make_tuple(-3,0,0,-3,"Invaild");
    }

}

MU_RETURNS sqlProvider::reLoadDB()
{
    try {
        SQLHelper::GetBlade()->_p_tr->ReLoad();
    } catch (...) {
        qWarning()<<__FUNCTION__<<"重新载入数据库失败!";
        return MU_RETURN_ERROR;
    }
    return MU_RETURN_SUCCESS;
}

MU_RETURNS sqlProvider::_open()
{
    try {
        std::string db_Path = SysMembers::GetInstance()->getDBPath();
        SQLHelper::GetBlade()->_p_tr->setDB(db_Path);
        SQLHelper::GetBlade()->_p_tr->setDBType("QSQLITE");
        SQLHelper::GetBlade()->_p_tr->ConnectToDB();
        return MU_RETURN_SUCCESS;
    } catch (...) {
        qWarning()<<BOLDRED<<__FUNCTION__<<"载入数据库失败,可能是路径名不存在或者权限问题!"<<RESET;
        throw  MU_EXPS::EXP_SQL_PATH;
        return MU_RETURN_ERROR;
    }
}

void sqlProvider::initBackThread()
{
//    std::thread *back = new std::thread([this](){
//        while (getSystemStatus()) {

//        }
//        qWarning()<<"系统关闭!"<<__FUNCTION__;
//    });

//    ThreadPool::GetInstance()->pushPermanentAsync(MU_THREAD_SAVE,back);
}


